#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "../include/map.h"
#include "../include/mine_map.h"

map *CreateMap(int mode) 
{
	map *Map = malloc(sizeof(map));
	if (Map == NULL)
		return NULL;
	switch (mode)
	{
	case MODE_EASY:
		Map->mx = MX_EASY;
		Map->my = MY_EASY;
		Map->cnt = NUM_EASY;
		break;
	case MODE_MID:
		Map->mx = MX_MID;
		Map->my = MY_MID;
		Map->cnt = NUM_MID;
		break;
	case MODE_HARD:
		Map->mx = MX_HARD;
		Map->my = MY_HARD;
		Map->cnt = NUM_HARD;
		break;
	default:
		break;
	}
	Map -> mode = mode;
	Map -> generated = false;
	return Map;
}

void Setmine(map *Map, int posx, int posy, unsigned int seed)
{
	
	for (int i = 0; i <= Map->mx; i++)
	{
		for (int j = 0; j <= Map->my; j++)
		{
			Map->autobook[i][j] = 0;
			Map->mineMap[i][j] = 0;
			Map->stage[i][j] = 0;
		}
	}
	int cnt = 0;
	srand(seed);
	while (cnt < Map->cnt)
	{
		int x, y;
		x = (rand() % (Map->mx)) + 1;
		y = (rand() % (Map->my)) + 1;
		if(abs(posx - x) <= 1 && abs(posy - y) <= 1) continue;
		if (Map->mineMap[x][y] == 0)
		{
			Map->mineMap[x][y] = 1;
			cnt++;
		}
	}
	Map -> generated = true;
#ifdef DEBUG
	puts("\nMap INFO:");
	printf("Seed: %u\n", seed);
	for (int i = 1; i <= Map->mx; i++)
	{
		for (int j = 1; j <= Map->my; j++) putchar(Map -> mineMap[i][j] + '0');
		putchar('\n');
	}
#endif
}
